package com.yanqu.road.dao.impl.cross.unionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionwar.cross.CrossUnionWarDetail;
import com.yanqu.road.entity.activity.unionwar.cross.UnionWarPatronsData;
import com.yanqu.road.entity.activity.unionwar.cross.UnionWarUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionWarDetailDaoImpl extends TempDataObjectDao<CrossUnionWarDetail> {


    public Map<Integer, Map<Integer, Map<Integer, List<CrossUnionWarDetail>>>> getCrossUnionWarDetailMap(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, Map<Integer, List<CrossUnionWarDetail>>>> dataMap = new ConcurrentHashMap<>();
        if(null == activityIdList || activityIdList.size() <= 0){
            return dataMap;
        }
        String sql = "select * from t_c_activity_unionwar_detail where activityId in(" + StringUtils.listToString(activityIdList, ",") + ") ";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossUnionWarDetail data = parse(rs);
                if(!dataMap.containsKey(data.getActivityId())){
                    dataMap.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                if(!dataMap.get(data.getActivityId()).containsKey(data.getStageNo())){
                    dataMap.get(data.getActivityId()).put(data.getStageNo(), new ConcurrentHashMap<>());
                }
                if(!dataMap.get(data.getActivityId()).get(data.getStageNo()).containsKey(data.getRoundNo())){
                    dataMap.get(data.getActivityId()).get(data.getStageNo()).put(data.getRoundNo(), new ArrayList<>());
                }
                dataMap.get(data.getActivityId()).get(data.getStageNo()).get(data.getRoundNo()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String insertSql = "insert into t_c_activity_unionwar_detail(activityId,stageNo,roundNo,unionUid,enemyUnionUid," +
            "modifyTime,matchSync,no,enemyNo,winMark,resultSync,isTopMatch,totalSilver,totalAbility,patronsData,userData,isAddScore,groupId) " +
            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossUnionWarDetail data) {
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStageNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoundNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
//        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyUnionUid()));
//        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getModifyTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getMatchSync(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnemyNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWinMark()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getResultSync(), ",")));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTopMatch()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTotalSilver().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTotalAbility().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getPatronsDataListJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUserDataMapJsonStr()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAddScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        return getDb().executeNoneQuery(insertSql, para) > 0;
    }

    public static String updateSql = "update t_c_activity_unionwar_detail set enemyUnionUid=?,modifyTime=?,matchSync=?,no=?,enemyNo=?," +
            "winMark=?,resultSync=?,isTopMatch=?,totalSilver=?,totalAbility=?,patronsData=?,userData=?,isAddScore=?,groupId=? where activityId=? and stageNo=? and roundNo=? " +
            " and unionUid=?";

    @Override
    protected boolean updateData(CrossUnionWarDetail data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyUnionUid()));
//        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getModifyTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getMatchSync(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnemyNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWinMark()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getResultSync(), ",")));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTopMatch()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTotalSilver().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTotalAbility().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getPatronsDataListJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUserDataMapJsonStr()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAddScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getStageNo()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoundNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
//        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected CrossUnionWarDetail parseData(ResultSet rs) throws SQLException {
        CrossUnionWarDetail data = new CrossUnionWarDetail();
        data.setActivityId(rs.getInt("activityId"));
        data.setStageNo(rs.getInt("stageNo"));
        data.setRoundNo(rs.getInt("roundNo"));
        data.setUnionUid(rs.getString("unionUid"));
//        data.setServerId(rs.getLong("serverId"));
        data.setEnemyUnionUid(rs.getString("enemyUnionUid"));
//        data.setEnemyServerId(rs.getLong("enemyServerId"));
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setMatchSync(StringUtils.stringToLongList(rs.getString("matchSync"), ","));
        data.setNo(rs.getInt("no"));
        data.setEnemyNo(rs.getInt("enemyNo"));
        data.setWinMark(rs.getInt("winMark"));
        data.setResultSync(StringUtils.stringToLongList(rs.getString("resultSync"), ","));
        data.setTopMatch(rs.getBoolean("isTopMatch"));
        String value = rs.getString("totalSilver");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setTotalSilver(new BigInteger(value));
        }else {
            data.setTotalSilver(BigInteger.ZERO);
        }
        value = rs.getString("totalAbility");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setTotalAbility(new BigInteger(value));
        }else {
            data.setTotalAbility(BigInteger.ZERO);
        }
        value = rs.getString("patronsData");
        if(!StringUtils.isNullOrEmpty(value)){
            List<UnionWarPatronsData> patronsDataList = JSON.parseObject(value, new TypeReference<List<UnionWarPatronsData>>() {});
            if(null != patronsDataList) {
                data.setPatronsDataList(patronsDataList);
            }
        }
        value = rs.getString("userData");
        if(!StringUtils.isNullOrEmpty(value)){
            Map<Long, UnionWarUserData> userDataMap = JSON.parseObject(value, new TypeReference<Map<Long, UnionWarUserData>>() {});
            if(null != userDataMap) {
                data.setUserDataMap(userDataMap);
            }
        }
        data.setAddScore(rs.getBoolean("isAddScore"));
        data.setGroupId(rs.getInt("groupId"));
        return data;
    }
}
